<?php

/**
 * This is the model class for table "iteam_product".
 *
 * The followings are the available columns in table 'iteam_product':
 * @property string $pid
 * @property integer $pcid
 * @property string $pname
 * @property string $pic
 * @property integer $price
 * @property integer $oldprice
 * @property string $briefing
 * @property string $description
 * @property integer $hongyun
 * @property integer $coin
 * @property integer $coin_discount
 * @property string $recommend
 * @property integer $hot
 * @property string $status
 * @property string $dateline
 * @property string $updated
 */
class Product extends CActiveRecord implements IECartPosition {
    
    public $pcname;

    /**
     * Returns the static model of the specified AR class.
     * @param string $className active record class name.
     * @return Product the static model class
     */
    public static function model($className = __CLASS__) {
        return parent::model($className);
    }

    /**
     * @return string the associated database table name
     */
    public function tableName() {
        return 'iteam_product';
    }
    
    function getId(){
        return $this->pid;
    }

    function getPrice(){
        return $this->price;
    }

    /**
     * @return array validation rules for model attributes.
     */
    public function rules() {
        // NOTE: you should only define rules for those attributes that
        // will receive user inputs.
        return array(
            array('pcid, price, oldprice, hongyun, coin, coin_discount, hot', 'numerical', 'integerOnly' => true),
            array('pname, pic, pic2', 'length', 'max' => 100),
            array('recommend, status, type', 'length', 'max' => 10),
            array('briefing, description, dateline, updated', 'safe'),
            // The following rule is used by search().
            // Please remove those attributes that should not be searched.
            array('pid, pcid, pname, pic, price, oldprice, briefing, description, hongyun, coin, coin_discount, recommend, hot, status, dateline, updated, type, pic2', 'safe', 'on' => 'search'),
        );
    }

    /**
     * @return array relational rules.
     */
    public function relations() {
        // NOTE: you may need to adjust the relation name and the related
        // class name for the relations automatically generated below.
        return array(
            'class' => array(self::BELONGS_TO, 'ProductClass', 'pcid'),
        );
    }

    /**
     * @return array customized attribute labels (name=>label)
     */
    public function attributeLabels() {
        return array(
            'pid' => 'PID',
            'pname' => '名稱',
            'pcid' => '分類ID',
            'pic' => '圖片',
            'pic2' => '內頁圖片',
            'price' => '價格',
            'oldprice' => '市場價',
            'briefing' => '簡介',
            'description' => '描述',
            'hongyun' => '鴻運點數',
            'coin' => '購物金',
            'coin_discount' => '抵扣購物金',
            'recommend' => '推薦',
            'hot' => '排序',
            'status' => '狀態',
            'type' => '類型',
            'dateline' => '創建日期',
            'updated' => '更新日期',
            'pcname' => '分類',
        );
    }

    /**
     * Retrieves a list of models based on the current search/filter conditions.
     * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
     */
    public function search() {
        // Warning: Please modify the following code to remove attributes that
        // should not be searched.

        $criteria = new CDbCriteria;
        
        $criteria->with = 'class';
        $criteria->compare('class.pcname', $this->pcname, true);

        $criteria->compare('pid', $this->pid, true);
        $criteria->compare('pcid', $this->pcid);
        $criteria->compare('pname', $this->pname, true);
        $criteria->compare('pic', $this->pic, true);
        $criteria->compare('pic2', $this->pic2, true);
        $criteria->compare('price', $this->price);
        $criteria->compare('oldprice', $this->oldprice);
        $criteria->compare('briefing', $this->briefing, true);
        $criteria->compare('description', $this->description, true);
        $criteria->compare('hongyun', $this->hongyun);
        $criteria->compare('coin', $this->coin);
        $criteria->compare('coin_discount', $this->coin_discount);
        $criteria->compare('recommend', $this->recommend, true);
        $criteria->compare('hot', $this->hot);
        $criteria->compare('status', $this->status, true);
        $criteria->compare('type', $this->type, true);
        $criteria->compare('dateline', $this->dateline, true);
        $criteria->compare('updated', $this->updated, true);

        return new CActiveDataProvider($this, array(
                    'criteria' => $criteria,
                ));
    }
    
    protected function beforeSave() {
        if (parent::beforeSave()) {
            if ($this->isNewRecord) {
                $this->dateline = $this->updated = date('Y-m-d H:i:s');
            }
            $this->updated = date('Y-m-d H:i:s');
        }
        return true;
    }

    public function afterFind() {
        parent::afterFind();
        $this->pcname = $this->class->pcname;
    }

}